package io.github.heollhai.common.config.security.handler;


import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.result.ResultCodeEnum;
import io.github.heollhai.common.utils.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: lmf
 * @Create: 2024/8/5 15:19
 * Description: 认证失败处理器
 */
@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {
        Result<String> result = Result.fail(ResultCodeEnum.LOGIN_EXPIRED);
        ResponseUtils.writeJson(response, result);
    }
}
